using CollabApp.Domain.Entities.Room;

namespace CollabApp.Domain.Services.Room;

/// <summary>
/// 房间服务接口
/// 负责房间的创建、管理、查询等业务逻辑
/// </summary>
public interface IRoomService
{
    /// <summary>
    /// 创建新房间
    /// </summary>
    /// <param name="name">房间名称</param>
    /// <param name="ownerId">房主用户ID</param>
    /// <param name="maxPlayers">最大玩家数</param>
    /// <param name="password">房间密码（可选）</param>
    /// <param name="isPrivate">是否私有房间</param>
    /// <param name="settings">房间设置JSON</param>
    /// <returns>创建的房间</returns>
    Task<Entities.Room.Room> CreateRoomAsync(string name, Guid ownerId, int maxPlayers = 4,
                                             string? password = null, bool isPrivate = false,
                                             string? settings = null);

    /// <summary>
    /// 获取房间列表
    /// </summary>
    /// <param name="pageNumber">页码</param>
    /// <param name="pageSize">每页大小</param>
    /// <param name="includePrivate">是否包含私有房间</param>
    /// <param name="statusFilter">状态过滤</param>
    /// <returns>房间列表</returns>
    Task<PagedResult<RoomListItem>> GetRoomListAsync(int pageNumber = 1, int pageSize = 20,
                                                     bool includePrivate = false,
                                                     RoomStatus? statusFilter = null);

    /// <summary>
    /// 获取房间详情
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="userId">请求用户ID</param>
    /// <returns>房间详情</returns>
    Task<RoomDetail?> GetRoomDetailAsync(Guid roomId, Guid userId);

    /// <summary>
    /// 加入房间
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="userId">用户ID</param>
    /// <param name="password">房间密码（如果需要）</param>
    /// <returns>加入结果</returns>
    Task<JoinRoomResult> JoinRoomAsync(Guid roomId, Guid userId, string? password = null);

    /// <summary>
    /// 离开房间
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="userId">用户ID</param>
    /// <returns>离开结果</returns>
    Task<LeaveRoomResult> LeaveRoomAsync(Guid roomId, Guid userId);

    /// <summary>
    /// 更新房间信息
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="userId">操作用户ID</param>
    /// <param name="name">新房间名称</param>
    /// <param name="maxPlayers">新最大玩家数</param>
    /// <param name="password">新密码</param>
    /// <param name="isPrivate">是否私有</param>
    /// <param name="settings">新设置</param>
    /// <returns>更新结果</returns>
    Task<UpdateRoomResult> UpdateRoomAsync(Guid roomId, Guid userId, string? name = null,
                                          int? maxPlayers = null, string? password = null,
                                          bool? isPrivate = null, string? settings = null);

    /// <summary>
    /// 删除房间
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="userId">操作用户ID</param>
    /// <returns>删除结果</returns>
    Task<DeleteRoomResult> DeleteRoomAsync(Guid roomId, Guid userId);

    /// <summary>
    /// 设置玩家准备状态
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="userId">用户ID</param>
    /// <param name="isReady">是否准备</param>
    /// <returns>设置结果</returns>
    Task<SetReadyResult> SetPlayerReadyAsync(Guid roomId, Guid userId, bool isReady);

    /// <summary>
    /// 开始游戏
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="userId">发起用户ID</param>
    /// <param name="username">用户名（可选，从JWT获取）</param>
    /// <returns>开始游戏结果</returns>
    Task<StartGameResult> StartGameAsync(Guid roomId, Guid userId, string? username = null);

    /// <summary>
    /// 检查房间是否存在
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <returns>是否存在</returns>
    Task<bool> RoomExistsAsync(Guid roomId);

    /// <summary>
    /// 获取用户当前所在的房间
    /// </summary>
    /// <param name="userId">用户ID</param>
    /// <returns>房间信息</returns>
    Task<RoomDetail?> GetUserCurrentRoomAsync(Guid userId);

    /// <summary>
    /// 踢出玩家
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="ownerId">房主ID</param>
    /// <param name="targetUserId">要踢出的用户ID</param>
    /// <returns>踢出结果</returns>
    Task<KickPlayerResult> KickPlayerAsync(Guid roomId, Guid ownerId, Guid targetUserId);

    /// <summary>
    /// 发送聊天消息
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="userId">发送用户ID</param>
    /// <param name="message">消息内容</param>
    /// <param name="messageType">消息类型</param>
    /// <returns>发送结果</returns>
    Task<SendChatMessageResult> SendChatMessageAsync(Guid roomId, Guid userId, string message, string messageType = "text");

    /// <summary>
    /// 获取房间聊天历史
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="userId">请求用户ID</param>
    /// <param name="limit">消息数量限制</param>
    /// <param name="offset">偏移量</param>
    /// <returns>聊天历史</returns>
    Task<GetChatHistoryResult> GetChatHistoryAsync(Guid roomId, Guid userId, int limit = 50, int offset = 0);
}

/// <summary>
/// 分页结果
/// </summary>
public class PagedResult<T>
{
    public List<T> Items { get; set; } = new();
    public int TotalCount { get; set; }
    public int PageNumber { get; set; }
    public int PageSize { get; set; }
    public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);
    public bool HasPreviousPage => PageNumber > 1;
    public bool HasNextPage => PageNumber < TotalPages;
}

/// <summary>
/// 房间列表项
/// </summary>
public class RoomListItem
{
    public Guid Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public string OwnerName { get; set; } = string.Empty;
    public int CurrentPlayers { get; set; }
    public int MaxPlayers { get; set; }
    public RoomStatus Status { get; set; }
    public bool IsPrivate { get; set; }
    public bool HasPassword { get; set; }
    public DateTime CreatedAt { get; set; }
    public string? GameMode { get; set; }
}

/// <summary>
/// 房间详情
/// </summary>
public class RoomDetail
{
    public Guid Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public Guid OwnerId { get; set; }
    public string OwnerName { get; set; } = string.Empty;
    public int CurrentPlayers { get; set; }
    public int MaxPlayers { get; set; }
    public RoomStatus Status { get; set; }
    public bool IsPrivate { get; set; }
    public bool HasPassword { get; set; }
    public DateTime CreatedAt { get; set; }
    public string? Settings { get; set; }
    public List<RoomPlayerInfo> Players { get; set; } = new();
}

/// <summary>
/// 房间玩家信息
/// </summary>
public class RoomPlayerInfo
{
    public Guid UserId { get; set; }
    public string UserName { get; set; } = string.Empty;
    public string? Avatar { get; set; }
    public bool IsReady { get; set; }
    public bool IsOwner { get; set; }
    public int? JoinOrder { get; set; }
    public string? PlayerColor { get; set; }
    public DateTime JoinedAt { get; set; }
}

/// <summary>
/// 加入房间结果
/// </summary>
public class JoinRoomResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public RoomDetail? RoomDetail { get; set; }
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 离开房间结果
/// </summary>
public class LeaveRoomResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public bool RoomDeleted { get; set; } = false;
    public Guid? NewOwnerId { get; set; } = null;
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 更新房间结果
/// </summary>
public class UpdateRoomResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public RoomDetail? RoomDetail { get; set; }
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 删除房间结果
/// </summary>
public class DeleteRoomResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 设置准备状态结果
/// </summary>
public class SetReadyResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public bool IsReady { get; set; }
    public bool CanStartGame { get; set; } = false;
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 开始游戏结果
/// </summary>
public class StartGameResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public Guid? GameId { get; set; }
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 踢出玩家结果
/// </summary>
public class KickPlayerResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 发送聊天消息结果
/// </summary>
public class SendChatMessageResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public ChatMessageInfo? MessageInfo { get; set; }
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 获取聊天历史结果
/// </summary>
public class GetChatHistoryResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public List<ChatMessageInfo> Messages { get; set; } = new();
    public int Total { get; set; }
    public int Limit { get; set; }
    public int Offset { get; set; }
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 聊天消息信息
/// </summary>
public class ChatMessageInfo
{
    public Guid Id { get; set; }
    public Guid RoomId { get; set; }
    public Guid UserId { get; set; }
    public string Username { get; set; } = string.Empty;
    public string Message { get; set; } = string.Empty;
    public string MessageType { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; }
}
